/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces;
using QuartzTypeLib;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using MyPhotoIndex.Interfaces.ImageFormatProviders;

namespace MyPhotoIndex.Plugins.ImageFormatProviders
{
    public class VideoPlayer : IVideoPlayer
    {
        public event EventHandler StatusChanged;

        private FilgraphManagerClass    m_fgmc;
        private Object                  m_tag;
        private VideoPlayerStatus       m_status = VideoPlayerStatus.Stopped;

        internal VideoPlayer(string fileName, IWin32Window parent, Rectangle position)
        {
            m_fgmc = new FilgraphManagerClass();
            m_fgmc.RenderFile(fileName);
            m_fgmc.Owner = (int)parent.Handle;
            m_fgmc.WindowStyle = WinStructs.WS_CHILD | WinStructs.WS_CLIPCHILDREN;

            Size calculatedSize = MyPhotoIndex.Utilities.ImageAlgo.GetImageProportionalSize(new SizeF(m_fgmc.VideoWidth, m_fgmc.VideoHeight), new SizeF(position.Width, position.Height));
            int leftOffset = (position.Width - calculatedSize.Width) / 2;
            m_fgmc.SetWindowPosition(position.Left + leftOffset, position.Top, calculatedSize.Width, calculatedSize.Height);
        }

        #region IVideoPlayer Members

        public void Play()
        {
            m_fgmc.Run();
            m_status = VideoPlayerStatus.Palying;

            if (StatusChanged != null)
            {
                StatusChanged.Invoke(this, EventArgs.Empty);
            }

            ThreadPool.QueueUserWorkItem(new WaitCallback(PumpMessages), null);
        }

        public void Stop()
        {
            m_fgmc.Stop();
            m_fgmc.Visible = 0;
            m_status = VideoPlayerStatus.Stopped;

            if (StatusChanged != null)
            {
                StatusChanged.Invoke(this, EventArgs.Empty);
            }
        }

        public Rectangle Position
        {
            get
            {
                int left, top, width, height;
                m_fgmc.GetWindowPosition(out left, out top, out width, out height);
                
                return new Rectangle(left, top, width, height);
            }
            set
            {
                Size calculatedSize = MyPhotoIndex.Utilities.ImageAlgo.GetImageProportionalSize(new SizeF(m_fgmc.VideoWidth, m_fgmc.VideoHeight), new SizeF(value.Width, value.Height));
                int leftOffset = (value.Width - calculatedSize.Width) / 2;
                m_fgmc.SetWindowPosition(value.Left + leftOffset, value.Top, calculatedSize.Width, calculatedSize.Height);
            }
        }

        public object Tag
        {
            get{return m_tag;}
            set{m_tag = value;}
        }

        internal void PumpMessages(Object state)
        {
            while (true)
            {
                Thread.Sleep(200);
                if (m_fgmc.Duration == m_fgmc.CurrentPosition)
                {
                    Stop();
                    break;
                }
            }
        }

        public VideoPlayerStatus Status
        {
            get { return m_status; }
        }

        #endregion
    }
}
